package edu.chl.dat076.datesite.entities;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 *
 * @author Christoffer
 */
@Embeddable
public class UserProperties implements Serializable {
	private static final long serialVersionUID = 1L;

	
	
	@Column(nullable = false)
	@Temporal(TemporalType.DATE)
	Date birthDate;
	@Column(nullable = false)
	@Enumerated(EnumType.STRING)
	Gender gender;
	int height;
	boolean tobaccoUser;
	boolean children;
	@Transient
	int age;
	
	@ManyToOne
	Town town;

	
	
	public UserProperties() {
            /*this.birthDate = new GregorianCalendar(1990,10,10);
            this.gender = Gender.FEMALE;
            this.height = 150;
            this.tobaccoUser = false;
            this.children = false;
            this.town = new Town("Vänersborg");*/

	}
	
	public UserProperties(Date birthDate, Gender gender) {
		this.birthDate = birthDate;
		this.gender = gender;
	}

	public UserProperties(Date birthDate, Gender gender, Town town) {
		this.birthDate = birthDate;
		this.gender = gender;
		this.town = town;
	}

	public UserProperties(Date birthDate, Gender gender, int height, boolean tobaccoUser, boolean children, Town town) {
		this.birthDate = birthDate;
		this.gender = gender;
		this.height = height;
		this.tobaccoUser = tobaccoUser;
		this.children = children;
		this.town = town;
	}

	
	
	public Date getBirthDate() {
		return birthDate;
	}

	public boolean isChildren() {
		return children;
	}

	public Gender getGender() {
		return gender;
	}

	public int getHeight() {
		return height;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public boolean isTobaccoUser() {
		return tobaccoUser;
	}

	public Town getTown() {
		return town;
	}
	
	public int getAge() {
		Calendar birth = new GregorianCalendar();
		birth.setTime(getBirthDate());
		Calendar now = new GregorianCalendar();
		now.setTime(new Date());
		int adjust = 0;
		if ((now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR)) < 0) {
			adjust = -1;
		}
		return now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + adjust;
	}
	
	

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public void setChildren(boolean children) {
		this.children = children;
	}

	public void setGender(Gender gender) {
		this.gender = gender;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void setTobaccoUser(boolean tobaccoUser) {
		this.tobaccoUser = tobaccoUser;
	}

	public void setTown(Town town) {
		this.town = town;
	}

	
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final UserProperties other = (UserProperties) obj;
		if (this.birthDate != other.birthDate && (this.birthDate == null || !this.birthDate.equals(other.birthDate))) {
			return false;
		}
		if (this.gender != other.gender) {
			return false;
		}
		if (this.height != other.height) {
			return false;
		}
		if (this.tobaccoUser != other.tobaccoUser) {
			return false;
		}
		if (this.children != other.children) {
			return false;
		}
		if (this.town != other.town && (this.town == null || !this.town.equals(other.town))) {
			return false;
		}
		return true;
	}

	
	
	@Override
	public int hashCode() {
		int hash = 7;
		return hash;
	}
}